package cn.cloud9.configuration;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {


    /**
     * 序列化配置
     *
     * @param lcf
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lcf) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        final StringRedisSerializer srs = new StringRedisSerializer();
        final GenericJackson2JsonRedisSerializer gj2jrs = new GenericJackson2JsonRedisSerializer();

        // 设置key键序列化对象
        redisTemplate.setKeySerializer(srs);

        // 设置value值序列化对象
        redisTemplate.setValueSerializer(gj2jrs);

        // 设置Hash类型的key序列化
        redisTemplate.setHashKeySerializer(srs);

        // 设置Hash类型的value序列化
        redisTemplate.setHashValueSerializer(gj2jrs);

        // 配置redis数据源
        redisTemplate.setConnectionFactory(lcf);

        return redisTemplate;
    }
}
